//
//  VIPMallWallet.swift
//  LeGou
//
//  Created by lxm on 2022/9/3.
//

import UIKit

class VIPMallWallet: ZMViewController {

    //MARK:--------------------属性--------------------
    let info = ZMUtil.fastNilBehavior(type: VIPMallModels.BalanceInfo.self)
    
    let walletInfo = ZMUtil.fastNilBehavior(type: MineModels.WalletInfo.self)

    //MARK:--------------------UI控件--------------------
    let loading = XMLoadingView()
    @IBOutlet weak var money: UILabel!
    
    @IBOutlet weak var estimateProfit: UILabel!
    
    @IBOutlet weak var withdrawProfit: UILabel!
    
    @IBOutlet weak var freeze: UILabel!
    @IBOutlet weak var navView: NavigationBarView!
    
    @IBOutlet weak var transContent: UIView!
    
    @IBOutlet weak var djzbzj: UILabel!
    
    @IBOutlet weak var subtips: UILabel!
    
    @IBOutlet weak var wxts: UILabel!
    
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
        loading.showInView(fatherView: view)
    }
    //MARK:--------------------UI--------------------
    func setUI() {
        hbd_barHidden = true
        navView.autoBack = true
        navView.titleLabel.text = "创客优选余额"
        navView.titleLabel.textColor = UIColor.zm.color333333()
        navView.titleLabel.font = .systemFont(ofSize: 18, weight: .semibold)
        navView.backButton.tintColor = UIColor.zm.color333333()
        
        if AppHelper.appVerify() {
            djzbzj.text = "未收货"
            wxts.isHidden = true
            subtips.isHidden = true
            transContent.isHidden = true
        } else {
            djzbzj.text = "冻结中(保证金)"
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadInfo()
        loadData()
    }
    
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        info.subscribe(onNext:{ [unowned self] resp in
            guard let resp = resp else {return}
            self.money.text = "\(resp.mallMoney.zm.toString(keep: 4))"
            self.estimateProfit.text = "\(resp.estimateMallMoney.zm.toString(keep: 4))"
            self.withdrawProfit.text = "\(resp.withdrawalMallMoney.zm.toString(keep: 4))"
            self.freeze.text = "\(resp.mallMoneyFreeze.zm.toString(keep: 4))"
        }).disposed(by: disposeBag)
        
        ZMNotification.shard.subject.subscribe(onNext:{ [unowned self] resp in
            switch resp {
                case .updateVIPMallBankInfo:
                    self.loadData()
                default:
                    break
            }
        }).disposed(by: disposeBag)
        
        walletInfo.subscribe(onNext:{ [unowned self] resp in
            if let resp = resp {
                let text = NSMutableAttributedString(string: resp.tips)
                let pa = NSMutableParagraphStyle()
                pa.lineSpacing = 8
                text.addAttributes([.paragraphStyle : pa], range: NSRange(location: 0, length: text.length))
                self.subtips.attributedText = text
            } else {
                self.subtips.text = ""
            }
        }).disposed(by: disposeBag)
    }
    
    func loadData() {
        VIPMallAPI.mallIncomeInfo([:]).rxRequest(type:VIPMallModels.BalanceInfo.self).subscribe(onNext:{ [unowned self] resp in
            self.loading.hideView(anmation: true)
            self.info.accept(resp)
        }, onError: { e in
            self.loading.showError(e.localizedDescription)
        }).disposed(by: disposeBag)
    }
    
    @IBAction func onWithdraw(_ sender: Any) {
        if LoginService.shared.checkRealName() == false {return}
        router.push("xm://main/Mine/withdraw", context: ["type" : 0])
    }
    
    @IBAction func onDetail(_ sender: Any) {
        router.push("xm://main/Mine/trade_detail", context: ["type" : 0])
    }
    
    @IBAction func onTransfer(_ sender: Any) {
        router.push("xm://main/Mine/TransferPage", context: ["type" : 2])
    }
    
    func loadInfo() {
        let api = VIPMallAPI.walletInfo([:])
        api.rxRequest(type: MineModels.WalletInfo.self).subscribe(onNext:{ [unowned self] resp in
            self.walletInfo.accept(resp)
        }, onError: { e in
        }).disposed(by: disposeBag)
    }
    //MARK:--------------------delegate--------------------

}
